import { ipcMain } from "electron";
import {
  hideMainWin,
  isMaximized,
  minimizeMainWin,
  unmaximizeMainWin,
  maximizeMainWin,
} from "../win/mainWin";

export const initIpcMain = () => {
  // 获取当前窗口
  // callback(BrowserWindow.fromWebContents(e.sender), e, ...par)
  const handle = (name: string, callback: Function) => {
    ipcMain.handle(name, (e, ...par) => callback(e, ...par));
  };

  // 关闭窗口
  handle("close", () => hideMainWin());

  // 最小化
  handle("min", () => minimizeMainWin());

  // 最大化
  handle("max", () => {
    let obj = { type: "max", data: "unmax" };
    if (isMaximized()) {
      unmaximizeMainWin();
    } else {
      obj.data = "max";
      maximizeMainWin();
    }
    return obj;
  });
};
